package com.authine.cloudpivot.web.Utils;

import com.authine.cloudpivot.web.api.exception.PortalException;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author chenjq
 */
public final class Util {

    private Util(){}

    public static String object2String(Object object) {
        if (object == null) {
            return null;
        } else {
            if ("".equals(object.toString().trim())) {
                return null;
            }
            return object.toString().trim();
        }
    }

    public static String object2StringNotNull(Object object) {
        if (object == null) {
            return "";
        } else {
            if ("".equals(object.toString().trim())) {
                return object.toString().trim();
            }
            return object.toString().trim();
        }
    }

    public static Double object2Double(Object object) {
        if (object == null) {
            return null;
        } else {
            return (null == object2String(object)) ? null : Double.parseDouble(object2String(object));
        }
    }

    public static Integer object2Integer(Object o) {
        if (o == null) {
            return null;
        } else {
            return (null == object2String(o)) ? null : Integer.parseInt((object2String(o).split("\\."))[0]);
        }
    }

    public static BigDecimal object2BigDecimal(Object object){
        if (object == null) {
            return null;
        } else {
            return (null == object2String(object)) ? null : new BigDecimal(object2String(object));
        }
    }

    public static Long object2Long(Object o) {
        if (o == null) {
            return null;
        } else {
            return (null == object2String(o)) ? null : Long.parseLong((object2String(o).split("\\."))[0]);
        }
    }

    public static String null2String(Integer o){
        if(o == null){
            return "";
        }else{
            return o+"";
        }
    }

    public static String null2String(Double o){
        if(o == null){
            return "";
        }else{
            return o+"";
        }
    }

    public static String null2String(String o){
        if (o == null) {
            return "";
        } else if ("null".equals(o)) {
            return "";
        }else {
            return o + "";
        }
    }

    /**
     * 将字符串首字母变大写
     * @param str 字符串
     * @return
     */
    public static String upperCaseFirstChar(String str) {
        str = str.substring(0, 1).toUpperCase() + str.substring(1);
        return  str;

    }

    /**
     * 将字符串首字母变小写
     * @param str 字符串
     * @return
     */
    public static String lowerCaseFirstChar(String str) {
        str = str.substring(0, 1).toLowerCase() + str.substring(1);
        return  str;

    }

    /**
     * 检查数据
     * @param dataElement sap返回数据
     * @return
     */
//    public static DataElement checkDataElement(DataElement dataElement){
//        if(dataElement == null){
//            dataElement = new DataElement();
//        }
//        return dataElement;
//    }


    /**
     * 将列表转成字符串，中间用逗号隔开
     * @return
     */
    public static String listToString(List<Object> list){
        StringBuilder stringBuilder=new StringBuilder();
        if(list!=null && list.size()>0) {
            stringBuilder.append(list.get(0).toString());
            for (int i=1;i<list.size();i++) {
                stringBuilder.append(",");
                stringBuilder.append(list.get(i).toString());
            }
            return stringBuilder.toString();
        }else{
            return "";
        }
    }

    /**
     * 将列表转成字符串，中间用逗号隔开
     * @return
     */
    public static String stringListToString(List<String> list){
        StringBuilder stringBuilder=new StringBuilder();
        if(list!=null && list.size()>0) {
            stringBuilder.append(list.get(0));
            for (int i=1;i<list.size();i++) {
                stringBuilder.append(",");
                stringBuilder.append(list.get(i).toString());
            }
            return stringBuilder.toString();
        }else{
            return "";
        }
    }

    public   static   void  removeDuplicateWithOrder(List list)   {
        Set set  =   new  HashSet();
        List newList  =   new  ArrayList();
        for  (Iterator iter  =  list.iterator(); iter.hasNext();)   {
            Object element  =  iter.next();
            if  (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        System.out.println( " remove duplicate "   +  list);
    }

    public static Boolean checkObjectIsNull(Object object){
        if(object == null || "".equals(object.toString().trim())){
            return true;
        }
        return  false;
    }

    /**
     * 数据转换，主要对boolean进行了转换
     * @param obj
     * @return
     */
//    public static Object dataTransform(Object obj) {
//        if(obj ==null){
//            return "";
//        }
//
//        if (obj instanceof Boolean) {
//            boolean value = (Boolean) obj;
//            if (value) {
//                return "是";
//            } else {
//                return "否";
//            }
//        }else if(obj instanceof ZonedDateTime){
//            return DateUtil.ZonedDateTimeToStr((ZonedDateTime) obj);
//        }
//        return obj;
//    }

    /**
     * 字符串转换时间格式yyyy/MM/dd
     * @return
     */
    public static Date string2Date(String time,int i){
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            throw new PortalException(500L,"第"+(i+1)+"行时间格式错误");
        }
        return date;
    }


    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","");
    }

    public static Map<String, Object> object2Map(Object obj) {
        Map<String, Object> map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(obj));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
